package com.gdlgxy.oa.controller;


import com.gdlgxy.oa.bean.StudentList;
import com.gdlgxy.oa.service.StudentServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

public class StudentCreateCtrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取要添加的学生姓名
        // 注意乱码问题
        request.setCharacterEncoding("UTF-8");

        // 获取添加的学生姓名
        String sname = request.getParameter("Sname");
        System.out.println("controller：添加的学生姓名为："+sname);

        // 获取学生链表
        StudentList list = StudentServer.list();
        // 添加学生
        list.snameAdd(sname);
        // 向Server层传递
        // 添加成功就转发回学生链表
        if (StudentServer.create(list)) {
            request.getRequestDispatcher("/student/list").forward(request, response);
        } else {
            // 删除失败
            request.getRequestDispatcher("/error.html").forward(request, response);
        }

    }
}
